package com.lanou.controller;

import com.lanou.bean.User;
import com.lanou.mapper.UserCollectionMapper;
import com.lanou.mapper.UserCommentMapper;
import com.lanou.realm.MyPerRealm;
import com.lanou.service.UserCollectionService;
import com.lanou.service.UserCommentService;
import com.lanou.service.UserService;
import com.lanou.utils.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by dllo on 18/3/14.
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserCommentService userCommentService;
    @Resource
    private UserCollectionService userCollectionService;

    /**
     * @param phone    注册用的手机号
     * @param nickname 昵称
     * @param password 用户密码
     * @return 注册成功与否返回值
     * 开发人:赵帅
     */
    @ResponseBody
    @RequestMapping("/registUser")
    public AjaxResult registUser(@RequestParam("phone") String phone, @RequestParam("nickname") String nickname, @RequestParam("password") String password) {
        return userService.registUser(phone, nickname, password);
    }

    /**
     * @param request
     * @return 登录成功与否返回值
     * @throws Exception 异常
     *                   开发人:赵帅
     */
    @ResponseBody
    @RequestMapping("/loginsubmit")
    public AjaxResult loginsubmit(HttpServletRequest request) throws Exception {
        AjaxResult ajaxResult = new AjaxResult();
        String exName = (String) request.getAttribute("shiroLoginFailure");
        if (UnknownAccountException.class.getName().equals(exName)) {
//            throw new UnknownAccountException("账号不存在");
            ajaxResult.setCode("500");
            ajaxResult.setMsg("账号错误");
            ajaxResult.setData(exName);
            return ajaxResult;
        } else if (IncorrectCredentialsException.class.getName().equals(exName)) {
//            throw new IncorrectCredentialsException("密码错误");
            ajaxResult.setCode("500");
            ajaxResult.setMsg("密码错误");
            ajaxResult.setData(exName);
            return ajaxResult;
        } else {
//            throw new Exception("我也不知道为啥不对");
            ajaxResult.setCode("500");
            ajaxResult.setMsg("未知原因错误");
            ajaxResult.setData(exName);
            return ajaxResult;
        }

    }

    /**
     * 用户添加评论功能
     *
     * @param state        施工状态
     * @param designNum    设计分数
     * @param serviceNum   服务分数
     * @param constructNum 施工分数
     * @param userContent  用户评论内容
     * @param productId    产品Id
     */
    @RequestMapping("/userComment")
    public void userComment(@RequestParam("state") String state, @RequestParam("designNum") Integer designNum,
                            @RequestParam("serviceNum") Integer serviceNum, @RequestParam("constructNum") Integer constructNum,
                            @RequestParam("userContent") String userContent, @RequestParam("productId") Integer productId,
                            @RequestParam("company_id") Integer company_id) {
        userCommentService.userComment(state, designNum, serviceNum, constructNum, userContent, productId, company_id);
    }

    /**
     * @param repeatContent 回复内容
     * @param id            回复哪条评论的id号
     *                      开发人:赵帅
     */
    @RequestMapping("/repeatComment")
    public void repeatComment(@RequestParam("repeatContent") String repeatContent, @RequestParam("id") Integer id) {
        userCommentService.repeatComment(repeatContent, id);
    }

    /**
     * @param albumName 创建的图集名称
     * @return 创建是否成功
     * 开发人:赵帅
     */
    @ResponseBody
    @RequestMapping("/createAlbum")
    public AjaxResult createAlbum(@RequestParam("albumName") String albumName) {
        return userCollectionService.createAlbum(albumName);
    }

    /**
     *
     * @param map_id 收藏图集的id
     * @param src 收藏图片的地址
     * @return 收藏的结果
     * 开发人:赵帅
     */
    @ResponseBody
    @RequestMapping("/insertMap")
    public AjaxResult insertmap(@RequestParam("map_id") Integer map_id,@RequestParam("src")String src) {
        return userCollectionService.insertMap(map_id,src);
    }

    /**
     *
     * @return 所有收藏图片集合
     * 开发人:赵帅
     */

     @ResponseBody
     @RequestMapping("/showCollectImg")
    public AjaxResult showCollectImg(){
     return  userCollectionService.showMap();
    }

    /**
     *
     * @param albumName 修改收藏图集后的名字
     * @return 修改结果
     * 开发人:赵帅
     */
    @ResponseBody
    @RequestMapping("/changName")
    public  AjaxResult changeCollectMapName(@RequestParam("albumName")String albumName,
                                            @RequestParam("map_id") Integer id){
        return userCollectionService.changeMapName(albumName,id);
    }

    /**
     *
     * @param id 专辑的id
     * @return 删除结果
     * 开发人:赵帅
     */
    @ResponseBody
    @RequestMapping("/deleteMap")
    public AjaxResult deleteCollectMap(@RequestParam("map_id")Integer id){
        return userCollectionService.deleteMap(id);
    }
    /**
     * @return 登录界面
     * 开发人:赵帅
     */
    @RequestMapping("/login")
    public String loginpage() {
        return "login";
    }

    /**
     * @return 跳转网页
     * 开发人赵帅
     */
    @RequestMapping("/comment")
    public String commentpage() {
        return "comment";
    }

    /**
     * 聊天页
     * @return
     */
    @RequestMapping("/chat")
    public String chat() {
        return "chat";
    }

}
